package com.itnoob.command1.example;

import com.itnoob.command1.example.service.CouponService;
import com.itnoob.command1.example.service.ProductService;
import com.itnoob.command1.example.service.StockService;
import com.itnoob.command1.example.task.GetCouponTask;
import com.itnoob.command1.example.task.GetProductTask;
import com.itnoob.command1.example.task.GetStockTask;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author zhoujun
 * @date 2025/3/18 23:21
 */
public class CommandPatternExample {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //1:创建一个线程池对象
        ExecutorService pool = Executors.newFixedThreadPool(4);

        Collection<Future<Object>> futures = new LinkedList<>();

        //记录开始时间
        //这样做的好处是,用户只需要2秒,基本就可以执行完所有的任务,如果串行需要6秒
        long startTime = System.currentTimeMillis();
        futures.add(pool.submit(new GetProductTask(new ProductService(),1)));
        futures.add(pool.submit(new GetStockTask(new StockService(),1)));
        futures.add(pool.submit(new GetCouponTask(new CouponService(),100)));

        for (Future<Object> future : futures) {
            Object result = future.get();
            System.out.println("Result:" + result);
        }

        long endTime = System.currentTimeMillis();
         long totalTime = endTime - startTime;
        System.out.println("Total time: "+  totalTime +  " ");
    }
}
